#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author:Summerday

import csv

import pyecharts.options as opts
from pyecharts.charts import Graph

nodes = []
edges = []
degs = {}

with open("csv/deg.csv", 'r', encoding='utf-8', newline='') as f:
	next(f)
	r = csv.reader(f)
	for row in r:
		degs[row[0]] = row[1]

print(degs)


with open("csv/nodes.csv", 'r', encoding='utf-8', newline='') as f:
	next(f)
	r = csv.reader(f)
	for row in r:
		node = {}
		node['person_id'] = row[0]
		node["female"] = '男' if row[1] == '1' else '女'
		node['name'] = row[2]
		node["index_year"] = row[3]
		node["addr_name"] = row[4]
		node["x"] = row[5]
		node["y"] = row[6]
		node['color'] = '#c71969'
		# node['show'] = False
		nodes.append(node)
		

print(nodes)

for node in nodes:
	# if node.get('female') == '男':
	# 	node['color'] = 'blue'
	# else:
	# 	node['color'] = 'red'
	node['deg'] = int(degs.get(node.get('person_id')))
	if node['deg'] > 5:
		node['show'] = True
		node['focus'] = True
		node['color'] = '#58FAAC'
	else:
		node['show'] = False
		node['focus'] = False
		node['color'] = '#00BFFF'
	
	
print(nodes)
with open("csv/edges.csv", 'r', encoding='utf-8', newline='') as f:
	next(f)
	r = csv.reader(f)
	for row in r:
		#sourceId,sourceName,assocType,targetId,assocDesc
		edge = {
			'source_id':row[0],
			'target_id':row[3],
			'attributes':{
				'type':row[2],
				'desc':row[4]
			}
		}
		edges.append(edge)
		
print(edges)


need_nodes = [
	{
		# 'x':node['x'],
		# 'y':node['y'],
		'id':node['person_id'],
		'name':node['name'],
		'symbolSize':node['deg']*4
		,
		#权重
		'label':{
			'color':'black',
			'position':"inside",
			# 'show':node['show'],
			# 'backgroundColor':'#01DFD7'
		},
		'itemStyle':{
			'normal':{
				'color':node['color']
			}
		},
		"focusNodeAdjacency":node['focus']
	}
	for node in nodes
]
need_edges = [
	{
		'source':edge['source_id'],
		'target':edge['target_id'],
		'value':edge['attributes']['desc']
	}
	for edge in edges
]

(
	Graph(init_opts=opts.InitOpts(width="1600px", height="800px"))
		.add(
		series_name="",
		nodes=need_nodes,
		links=need_edges,
		layout="circular",
		is_roam=True,
		is_draggable=True,
		symbol='circle',
		# is_focusnode=False,
		edge_label=opts.LabelOpts(is_show=False,position='middle',formatter='{a}-{b}-{c}-{d}'),
		label_opts=opts.LabelOpts(is_show=True,font_family="KaiTi"),
		linestyle_opts=opts.LineStyleOpts(width=2, curve=0.3, opacity=0.7),
	)
		.set_global_opts(title_opts=opts.TitleOpts(
		title="明代学术关系图",
		subtitle = '绝对点度中心度分析',
		title_textstyle_opts = opts.series_options.TextStyleOpts(font_family = "KaiTi"),
		subtitle_textstyle_opts= opts.series_options.TextStyleOpts(font_family = "KaiTi")
		
	))
		.render("html/npm_dependencies_circle.html")
)

